import pymysql
import re,json
def doWork():
    num = input("input chu ku dan number: ")
    customer = input("input customer name: ")
    address = input("input customer address: ")
    tel = input("input customer tel: ")
    rows = list()
    while True:
        row = input("input chu ku huo wu like name price total(x exit): ")
        if row in ["x","q","X","Q",""]:
            break
        pattern = r"(?P<name>[^#@/]+)[#@/](?P<price>[^#@/]+)[#@/](?P<total>[^#@/]+)"
        match = re.search(pattern,row)
        if match:
            rows.append({"name":match.group("name"),"price":match.group("price"),"total":match.group("total")})
    details = json.dumps(rows)
    # save record
    record = (f"YH{int(num):08d}",customer,address,tel,details)
    print(record)
    cursor.execute("insert into tbchuku (num,customer,address,tel,details) values(%s,%s,%s,%s,%s);",record)
    conn.commit()
    cursor.close()
    conn.close()


if __name__ == "__main__":
    # 连接数据库 并创建表格
    conn = pymysql.connect(
        host="yhdnserver.cn",
        user="admin",password="1",
        database="world",
        cursorclass=pymysql.cursors.DictCursor
        )
    cursor = conn.cursor()
    cursor.execute("""
                   create table if not exists tbchuku(
                   id int primary key auto_increment,
                   num varchar(12)  not null unique,
                   customer varchar(100) not null,
                   address varchar(100) not null,
                   tel varchar(20) not null,    
                   createAt datetime default(now()),
                   details JSON not null);
                   """)
    doWork()